package com.rdzn.mall.portal.controller;

import com.rdzn.mall.base.common.Result;
import com.rdzn.mall.base.common.ResultCode;
import com.rdzn.mall.base.exception.Asserts;
import com.rdzn.mall.center.entity.UmsFeedback;
import com.rdzn.mall.center.entity.UmsMember;
import com.rdzn.mall.center.service.IUmsFeedbackService;
import com.rdzn.mall.portal.domain.AuthParam;
import com.rdzn.mall.portal.domain.LoginInfo;
import com.rdzn.mall.portal.service.IUserService;
import com.rdzn.mall.portal.weixin.WechatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author zhigangding
 * @date 2019/11/11
 */
@Controller
@Api(tags = "UserController", description = "首页")
@RequestMapping("/user")
public class MUserController {

    @Autowired
    private WechatService wechatService;
    @Autowired
    private IUmsFeedbackService feedbackService;
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "微信登陆")
    @RequestMapping(value = "/loginByWeixin", method = RequestMethod.POST)
    @ResponseBody
    public Result loginByWeixin(@RequestBody AuthParam param) {
        if (StringUtils.isEmpty(param.getCode())){
            return Result.error(ResultCode.VALIDATE_FAILED.getMessage());
        }
        LoginInfo jwtResponse = wechatService.wechatLogin(param);
        return Result.ok(jwtResponse);
    }

    @ApiOperation(value = "用户反馈")
    @RequestMapping(value = "/feedback", method = RequestMethod.POST)
    @ResponseBody
    public Result feedback(@RequestBody UmsFeedback feedback) {
        UmsMember member = userService.getCurrentMember();
        feedback.setCreateTime(new Date());
        feedback.setMemberId(member.getId());
        feedback.setMemberName(member.getNickname());
        if (StringUtils.isEmpty(feedback.getPhone())){
            feedback.setPhone(member.getPhone());
        }
        return Result.ok(feedbackService.save(feedback));
    }

    /**
     * 通过id查询
     *
     * @return
     */
    @AutoLog(value = "pms_product-通过id查询")
    @ApiOperation(value="pms_product-通过id查询", notes="pms_product-通过id查询")
    @GetMapping(value = "/info")
    @ResponseBody
    public Result<?> info() {
        UmsMember member = userService.getCurrentMember();
        if (member == null){
            Asserts.fail(ResultCode.UNAUTHORIZED);
        }
        return Result.ok(userService.getUmsUserInfo(member.getId()));
    }




}
